package com.nf.bookstytem.dao.readermanage;

import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.nf.bookstytem.dao.bookmanage.BookManageDaoImpl;

import com.nf.bookstytem.model.readermanage.ReaderModel;
import com.nf.bookstytem.uitl.QueryRunnerWrapper;

public class ReaderManageDaoImpl {
	public List<ReaderModel>getAll(){
		String sql="select * from reader order by rid";
		List<ReaderModel>readers=QueryRunnerWrapper.query(sql,new BeanListHandler<ReaderModel>(ReaderModel.class));
		return readers;
	}
	
	public int deleteReaderById(int id) {
		String sql="delete from reader where rreadid=?";
		int n=QueryRunnerWrapper.update(sql, id);
		return n;
	}
	
	public int insertReader(ReaderModel reader) {
		String sql="insert into reader values(?,?,?,?,?,?,?,?,?,?,?)";
		int n=QueryRunnerWrapper.update(sql,reader.getRreadid(),reader.getRloginid(),reader.getRloginpwd(),reader.getRname(),
				reader.getRphone(),reader.getRemail(),reader.getRregtime(),reader.getRcard(),
				reader.getRaddress(),reader.getRborrow(),reader.getRoperator());
		return n;
	}
	
	public int updateReader(ReaderModel reader) {
		String sql="update reader set rloginid=?,rloginpwd=?, rname=?, rphone=?,remail=?, rregtime=?, rcard=?, raddress=?, rborrow=?, roperator=? where rreadid=?";
		int n=QueryRunnerWrapper.update(sql,reader.getRloginid(),reader.getRloginpwd(),reader.getRname(),reader.getRphone(),reader.getRemail(),reader.getRregtime(),
				reader.getRcard(),reader.getRaddress(),reader.getRborrow(),reader.getRoperator(),reader.getRreadid());
		return n;
		
	}
	
	 public List<ReaderModel> PageList(int pageNo,int pageSize){
			String sql="select * from (select ROW_NUMBER()over(order by rreadid)rowNo,* from reader)t where RowNo between "+((pageNo-1)*pageSize+1)+" and "+(pageNo*pageSize)+"";
			List<ReaderModel> list=QueryRunnerWrapper.query(sql,  new BeanListHandler<ReaderModel>(ReaderModel.class));
			return list;
		}
		
	public int getPages() {
		String sql="select count(*) from reader";
		int n=QueryRunnerWrapper.queryCount(sql, new ScalarHandler<>(1));
		return n;
	}

	public static void main(String[] args) {
		ReaderManageDaoImpl dao=new ReaderManageDaoImpl();
		int pageNo=2;
		int pagesize=3;
		System.out .println(dao.PageList(pageNo,pagesize));
		}
}
